package com.xntech.ShowNetwork;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ShowNetwork extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        StringBuilder sb = new StringBuilder();
        sb.append("Show Network Info <www.xntech.com.cn>");
        
    	sb.append("\n\n\nThe network interface info of your device is:\n\n");
        final Enumeration<NetworkInterface> nics;
        try {
            nics = NetworkInterface.getNetworkInterfaces();
            if (null != nics) {
                while (nics.hasMoreElements()) {
                    final NetworkInterface ni = nics.nextElement();
                	sb.append("net interface=");
                	sb.append(ni.getDisplayName());
                	sb.append("   ip=");
                    sb.append(ni.getInetAddresses().nextElement().getHostAddress().toString());
                    sb.append("\n");
                }
            }
        } catch (SocketException e) {
            Log.e("ShowNetwork", "Error occured in network:", e);
        }
        
        ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); 
        if ( cm == null){
        	sb.append("Can not get ConnectivityManager!");
        }else{
        	sb.append("\n\n\nThe network info of your device is:\n\n");
            NetworkInfo[] ni = cm.getAllNetworkInfo();
            for (int i=0; i<ni.length; i++){
            	sb.append(ni[i].toString()+"\n");
            }
        }
    	
        sb.append("\n\nThe dhcp info of your device is:\n\n");
    	DhcpInfo di = new DhcpInfo();
    	sb.append(di.toString());
    	
        TextView tv = (TextView)findViewById(R.id.tv01);
        tv.setText(sb);
    }
}